package com.shuizhu.template.demo2;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.shuizhu.template.StudentByFill;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MultilineFileByList {
  
  public static void main(String[] args) {
    //要填充的Excel模板的路径
    String templatePath = "C:\\Users\\tianyunjie\\Desktop\\temp\\fill.xlsx";
    //填充后文件的保存路径
    String savePath = "C:\\Users\\tianyunjie\\Desktop\\temp\\fill-demo.xlsx";
    //根据模板获取工作簿
    ExcelWriterBuilder workBook = EasyExcel.write(savePath, StudentByFill.class).withTemplate(templatePath);
    //获取工作表
    ExcelWriterSheetBuilder sheet = workBook.sheet();
    //获取要填充的数据源
    List<StudentByFill> fillDataList = getFillDataList();
    //填充模板
    sheet.doFill(fillDataList);
  }
  
  /**
   * 生成要用于填充的数据
   */
  public static List<StudentByFill> getFillDataList() {
    List<StudentByFill> list = new ArrayList<>();
    
    for (int i = 1; i <= 10; i++) {
      list.add(new StudentByFill(String.valueOf((int) (Math.random() * 1000_0000) + 1000_0000), "张" + i, new Date(), Math.random() < 0.5 ? "男" : "女", Math.random() * 1_0000, "信息工程学院"));
    }
    return list;
  }
  
}

